// the middle place is the best; my solution runs as the 2nd fastest
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

vector<int> s;

int main()
{
	int ntest;
	cin >> ntest;
	while (0 < ntest--)
	{
		int r;
		cin >> r;
		s.clear();
		for (int i = 0; i < r; i++){
			int x;
			cin >> x;
			s.push_back(x);
		}

		sort(s.begin(), s.end());

		int v = s[r/2], sum = 0;
		for (int j = 0; j < (int) s.size(); j++) sum += abs(v-s[j]);
		cout << sum << endl;
	}
	return 0;
}
